FROM ubuntu:20.04

ARG ARCH
ARG GAUSSHOME=/usr/local/mogdb

WORKDIR /warehouse

RUN set -eux; \
    if [ "$ARCH" = "arm64" ]; then \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-security main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirror.tuna.tsinghua.edu.cn/ubuntu-ports/ focal-proposed main restricted universe multiverse" >>/etc/apt/sources.list; \
    elif [ "$ARCH" = "amd64" ]; then \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse" >/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse" >>/etc/apt/sources.list && \
        echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse" >>/etc/apt/sources.list; \
    fi && \
    apt-get update && apt-get install -y \
    wget \
    make && \
    if [ "$ARCH" = "arm64" ]; then \
      wget https://gitee.com/lee1002/gosu/attach_files/944222/download/gosu-arm64 -O gosu; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/MogDB-3.1.0-openEuler-arm64.tar.gz -O mogdb.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/dblink-1.0-3.1.0-01-openEuler-arm64.tar.gz -O dblink.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/mysql_fdw-1.1-3.1.0-01-openEuler-arm64.tar.gz -O mysql_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/oracle_fdw-1.0-3.1.0-01-openEuler-arm64.tar.gz -O oracle_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/orafce-3.17-3.1.0-01-openEuler-arm64.tar.gz -O orafce.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/pg_bulkload-1.0-3.1.0-01-openEuler-arm64.tar.gz -O pg_bulkload.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/pg_prewarm-1.1-3.1.0-01-openEuler-arm64.tar.gz -O pg_prewarm.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/pg_repack-1.4.6-3.1.0-01-openEuler-arm64.tar.gz -O pg_repack.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/pg_trgm-1.0-3.1.0-01-openEuler-arm64.tar.gz -O pg_trgm.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/postgres_fdw-1.0-3.1.0-01-openEuler-arm64.tar.gz -O postgres_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/wal2json-2.3-3.1.0-01-openEuler-arm64.tar.gz -O wal2json.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_arm64/plugins/whale-3.1.0-01-openEuler-arm64.tar.gz -O whale.tar.gz; \
    elif [ "$ARCH" = "amd64" ]; then \
      wget https://gitee.com/lee1002/gosu/attach_files/943635/download/gosu-amd64 -O gosu; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/MogDB-3.1.0-openEuler-x86_64.tar.gz -O mogdb.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/dblink-1.0-3.1.0-01-openEuler-x86_64.tar.gz -O dblink.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/mysql_fdw-1.1-3.1.0-01-openEuler-x86_64.tar.gz -O mysql_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/oracle_fdw-1.0-3.1.0-01-openEuler-x86_64.tar.gz -O oracle_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/orafce-3.17-3.1.0-01-openEuler-x86_64.tar.gz -O orafce.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/pg_bulkload-1.0-3.1.0-01-openEuler-x86_64.tar.gz -O pg_bulkload.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/pg_prewarm-1.1-3.1.0-01-openEuler-x86_64.tar.gz -O pg_prewarm.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/pg_repack-1.4.6-3.1.0-01-openEuler-x86_64.tar.gz -O pg_repack.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/pg_trgm-1.0-3.1.0-01-openEuler-x86_64.tar.gz -O pg_trgm.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/postgres_fdw-1.0-3.1.0-01-openEuler-x86_64.tar.gz -O postgres_fdw.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/wal2json-2.3-3.1.0-01-openEuler-x86_64.tar.gz -O wal2json.tar.gz; \
      wget https://cdn-mogdb.enmotech.com/mogdb-media/3.1.0/openEuler_x86_64/plugins/whale-3.1.0-01-openEuler-x86_64.tar.gz -O whale.tar.gz; \
    fi && \
    wget https://gitee.com/enmotech/compat-tools/attach_files/1110114/download/compat-tools-v2022.06.28.tar -O compat-tools-v2022.06.28.tar && \
    wget https://gitee.com/enmotech/mogila/attach_files/954052/download/mogila-v1.0.0.bz2 -O mogila-v1.0.0.bz2 && \
    mkdir -p $GAUSSHOME && \
    tar xf mogdb.tar.gz --strip-components 1 && tar xf MogDB-3.1.0-openEuler-64bit.tar.gz -C $GAUSSHOME --strip-components 1 && \
    rm -f mogdb.tar.gz && \
    tar xf dblink.tar.gz && rm -f dblink.tar.gz && cd dblink && make && cd .. && \
    tar xf mysql_fdw.tar.gz && rm -f mysql_fdw.tar.gz && cd mysql_fdw && make && cd .. && \
    tar xf oracle_fdw.tar.gz && rm -f oracle_fdw.tar.gz && cd oracle_fdw && make && cd .. && \
    tar xf orafce.tar.gz && rm -f orafce.tar.gz && cd orafce && make && cd .. && \
    tar xf pg_bulkload.tar.gz && rm -f pg_bulkload.tar.gz && cd pg_bulkload && make && cd .. && \
    tar xf pg_prewarm.tar.gz && rm -f pg_prewarm.tar.gz && cd pg_prewarm && make && cd .. && \
    tar xf pg_repack.tar.gz && rm -f pg_repack.tar.gz && cd pg_repack && make && cd .. && \
    tar xf pg_trgm.tar.gz && rm -f pg_trgm.tar.gz && cd pg_trgm && make && cd .. && \
    tar xf postgres_fdw.tar.gz && rm -f postgres_fdw.tar.gz && cd postgres_fdw && make && cd .. && \
    tar xf wal2json.tar.gz && rm -f wal2json.tar.gz && cd wal2json && make && cd .. && \
    tar xf whale.tar.gz && rm -f whale.tar.gz && cd whale && make && cd .. && \
    chmod 755 gosu && \
    tar xf compat-tools-v2022.06.28.tar && mv compat-tools-v2022.06.28 compat-tools && \
    tar xf mogila-v1.0.0.bz2

    COPY base/entrypoint.sh /usr/local/bin/